package com;



import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.Cookie;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class LoginServlet
 */
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	
		
	}
	

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	
		try { 
			
			//String a=request.getParameter("un");
			//String b=request.getParameter("pw");
			//if(a.equals("Login"))
			//{
			System.out.println("success123");
			UserBean user = new UserBean(); 
			user.setUserName(request.getParameter("un")); 
			user.setPassword(request.getParameter("pw")); 
			user= UserDAO.login(user);
			if(user.isValid())
			{
				
				
				
				String a = user.getUsername(); 
				String b = user.getPassword(); 
				
				
				Cookie loginCookie = new Cookie("user",a);
				//setting cookie to expiry in 30 mins
				loginCookie.setMaxAge(60*60*24*7);
				response.addCookie(loginCookie);
				
				request.setAttribute("us",a);
				request.setAttribute("ps",b);
				HttpSession session=request.getSession(true);
				session.setAttribute("CurrentSessionUser",user);
				RequestDispatcher requestDispatcher;
				requestDispatcher =request.getRequestDispatcher("HomePage.jsp");
				requestDispatcher.forward(request, response);
			//RequestDispatcher rd=request.getRequestDispatcher("HomePage.jsp");
		//rd.forward(request, response);
			}
			else
			{
				RequestDispatcher rd= request.getRequestDispatcher("Login.jsp");
				PrintWriter out= response.getWriter();
				out.println("<font color=red ><center>Either PAN number or Password is wrong.<center></font>");
				rd.include(request, response);
				
			}
			//}
		/*	else if(a.equals("Register Yourself"))
			{
				RequestDispatcher rd=request.getRequestDispatcher("Register.jsp");
				rd.forward(request, response);
			
				
			}*/
			/*if (user.isValid()) 
			{
				
				System.out.println("success.....");
				HttpSession session = request.getSession(true);
				session.setAttribute("currentSessionUser",user);
				//response.sendRedirect("SalesVoucher.jsp");
				//logged-in page 
				}
			else response.sendRedirect("invalidLogin.jsp");
			//error page*/
			} 
		
		catch (Throwable theException) 
		{ 
			System.out.println(theException); 
			} 
		
	
	}

}
